package com.wy.projectssj.dao.impl;

import com.wy.projectssj.dao.PermissionDao;
import com.wy.projectssj.domain.Permission;
import com.wy.projectssj.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: wy
 * @Date:2019/8/30 14:01
 */
@Repository
public class PermissionDaoImpl implements PermissionDao {
    @Autowired
    private JdbcTemplate jt;

    public List<Permission>  findAll(){
          String sql="select * from permission";
        List<Permission> list=null;
        try {
          list = jt.query(sql, new BeanPropertyRowMapper<>(Permission.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int save(Permission permission) {
        int result=-1;
        String sql="insert into permission values(?,?,?)";
        try {
            result = jt.update(sql,permission.getId(),permission.getPermissionName(),permission.getUrl());
        } catch (DataAccessException e) {

        }
        return result;
    }


    public int delete(String[] ids) {
        int result=-1;
        String s="delete from permission where 1=1 and id in (";
        StringBuilder sql=new StringBuilder(s);
        for (int i = 0; i <ids.length ; i++) {
            if(i==ids.length-1){
                sql.append("?)");
            }else{
                sql.append("?,");
            }
        }
        try {
            result = jt.update(sql.toString(), ids);
        } catch (DataAccessException e) {

        }
        return result;
    }
}
